package cao.oschina.bean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * 用户
 * <p/>
 * Created by Haidy on 15/4/5.
 */
public class User {

    public String uid;
    public String location;
    public String name;
    public String portrait;
    public String jointime;
    public String gender;
    public String score;
    public String from;
    public String devplatform;
    public String expertise;
    public String favoritecount;
    public String fans;
    public String followers;

    public static User parser(String xml) {
        User user = new User();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            Node noticeNode = root.getElementsByTagName("user").item(0);
            NodeList list = noticeNode.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Node node = list.item(i);
                    switch (node.getNodeName()) {
                        case "uid":
                            user.uid = node.getFirstChild().getNodeValue();
                            break;
                        case "location":
                            user.location = node.getFirstChild().getNodeValue();
                            break;
                        case "name":
                            user.name = node.getFirstChild().getNodeValue();
                            break;
                        case "portrait":
                            if (node.hasChildNodes()) {
                                user.portrait = node.getFirstChild().getNodeValue();
                            }
                            break;
                        case "jointime":
                            user.jointime = node.getFirstChild().getNodeValue();
                            break;
                        case "gender":
                            user.gender = node.getFirstChild().getNodeValue();
                            break;
                        case "score":
                            user.score = node.getFirstChild().getNodeValue();
                            break;
                        case "from":
                            user.from = node.getFirstChild().getNodeValue();
                            break;
                        case "devplatform":
                            user.devplatform = node.getFirstChild().getNodeValue();
                            break;
                        case "expertise":
                            user.expertise = node.getFirstChild().getNodeValue();
                            break;
                        case "favoritecount":
                            user.favoritecount = node.getFirstChild().getNodeValue();
                            break;
                        case "fans":
                            user.fans = node.getFirstChild().getNodeValue();
                            break;
                        case "followers":
                            user.followers = node.getFirstChild().getNodeValue();
                            break;
                        default:
                            break;
                    }
                }
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return user;
    }

    @Override
    public String toString() {
        Class c = this.getClass();
        Field[] fields = c.getDeclaredFields();
        String str = "";
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                str += field.getName() + "：" + field.get(this) + "\n";
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

}
